//
//  HNHNHappyWishController.m
//  XDEducation
//
//  Created by 星道 on 17/5/24.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyWishController.h"
#import "CustomSegmentedControl.h"
#import "HNHappyWishTableView.h"
#import "HNHappyWishSmallTarget.h"
#import "HNHappyWishBigTarget.h"
#import "WebViewController.h"

@interface HNHappyWishController ()

@property (nonatomic, strong) HNHappyWishTableView *TableView;
@property (nonatomic, strong) CustomSegmentedControl *Segment;
@property (nonatomic, strong) NSMutableArray *DataArr;
@property (nonatomic, strong) UIButton *AddButton;
@property (nonatomic, assign) NSInteger Index;
@end

@implementation HNHappyWishController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor(240, 240, 240);
    
    [self CreatUI];
    
    [self RefreshDataWithIndex:self.Index];
}



#pragma mark - 配置界面
- (void)CreatUI
{
    //选择栏
    [self.view addSubview:self.Segment];
    //表单
    [self.view addSubview:self.TableView];
    //添加目标
    [self.view addSubview:self.AddButton];
}





#pragma mark - 网络请求
- (void)RefreshDataWithIndex:(NSInteger)Index
{
    [MBProgressHUD showHUD];
    [XDHttpTool get:BaseURL([self GetActionWithIndex:Index]) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:nil success:^(id responseObj) {
        
        if (Index == 0)
        {
            if ([responseObj isKindOfClass:[NSArray class]])
            {
                for (NSDictionary *Dict in responseObj)
                {
                    [self.DataArr addObject:[[HNHappyWishSmallTarget alloc] init_HNObjectWithDict:Dict]];
                }
                [self TableViewReloadDataWithIndex:Index];
            }
            else
            {
                [MBProgressHUD showError:responseObj[@"data"][@"msg"]];
            }
        }
        
        if (Index == 1)
        {
            if ([[responseObj allKeys] count] >= 2)
            {
                for (NSDictionary *Dict in responseObj[@"bigtargets"])
                {
                    HNHappyWishBigTarget *Model = [[HNHappyWishBigTarget alloc] init_HNObjectWithDict:Dict];
                    Model.flowers_total = responseObj[@"flowers_total"];
                    [self.DataArr addObject:Model];
                }
                [self TableViewReloadDataWithIndex:Index];
            }
            else
            {
                [MBProgressHUD showError:responseObj[@"data"][@"msg"]];
            }
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}

- (NSString *)GetActionWithIndex:(NSInteger)Index
{
    if (Index == 0)
    {
        return [NSString stringWithFormat:@"user.do?action=getMySmallTarget&uid=%@",[AccountTool shareAccountTool].currentAccount.uid];
    }
    else if (Index == 1)
    {
        return [NSString stringWithFormat:@"user.do?action=getMyBigTarget2&uid=%@",[AccountTool shareAccountTool].currentAccount.uid];
    }
    else
    {
        return nil;
    }
}



#pragma mark - Function
/** 单击了Segment */
- (void)SelectSegmentItemWithIndex:(NSInteger)Index
{
    [self.DataArr removeAllObjects];
    [self TableViewReloadDataWithIndex:Index];
    [self RefreshDataWithIndex:Index];
}

/** 刷新界面 */
- (void)TableViewReloadDataWithIndex:(NSInteger)Index
{
    [self.TableView RefreshDataWithArr:self.DataArr Index:Index];
}

/** 查看图片 */
- (void)SeeImgWithUrlStr:(NSString *)UrlStr
{
    WebViewController *VC = [[WebViewController alloc] init];
    VC.str = UrlStr;
    [self.navigationController pushViewController:VC animated:YES];
}





#pragma mark - Setter
- (CustomSegmentedControl *)Segment
{
    if (!_Segment)
    {
        //选择栏
        CustomSegmentedControl *Segment = [[CustomSegmentedControl alloc] initWithItems:@[@"小目标",@"大目标"] onClickIndex:^(NSInteger index) {
            
            //切换事件
            [self SelectSegmentItemWithIndex:self.Index = index];
        }];
        Segment.frame = CGRectMake(0, 10, ScreenW, 40);
        Segment.selectIndex = 0;
        Segment.titleColor = UIColorFromRGB(0x4c4c4c);
        Segment.titleSelectColor = UIColorFromRGB(0x4c4c4c);
        Segment.selectedColor = ThemeColor;
        Segment.times = 0.05;
        self.Segment = Segment;
    }
    return _Segment;
}

- (HNHappyWishTableView *)TableView
{
    if (!_TableView)
    {
        CGFloat Y = self.Segment.frame.size.height+self.Segment.frame.origin.y;
        _TableView = [[HNHappyWishTableView alloc] initWithFrame:CGRectMake(32, Y, ScreenW-64, ScreenH-54-Y-64) style:(UITableViewStylePlain)];
        WeakSelf(WeakSelf)
        _TableView.SeeImg = ^(NSString *UrlStr)
        {
            [WeakSelf SeeImgWithUrlStr:UrlStr];
        };
    }
    return _TableView;
}

- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}

- (UIButton *)AddButton
{
    if (!_AddButton)
    {
        _AddButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _AddButton.frame = CGRectMake(16, ScreenH-44-64, ScreenW-32, 44-10);
        _AddButton.backgroundColor = ThemeColor;
        _AddButton.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_AddButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_AddButton setTitle:@"添加目标" forState:(UIControlStateNormal)];
    }
    return _AddButton;
}
@end
